class TasksController < ApplicationController
  # GET /tasks
  # GET /tasks.xml

  before_filter :resolve_criteries

  FILTERED_MODEL = 'tasks'

  def resolve_criteries
#    session[:query] = nil
    if session[:query].nil? or session[:query][FILTERED_MODEL].nil?
      new_query = Query.new(FILTERED_MODEL)

      new_query.foreign_keys = { 'submitter_id' => { :alias => 'submitter',
          :collection => User.find(:all).collect{ |u| [u.name, u.id.to_s] } } }

      session[:query] = { FILTERED_MODEL => new_query }
    end
    @query = session[:query][FILTERED_MODEL]
  end

  include FilterEngine

  def index
    @tasks = Task.find(:all, :conditions => @query.to_sql)
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @tasks }
    end
  end

  def exec_query
    @query.fill request

    respond_to do |format|
      format.html { redirect_to(tasks_url) }
    end
  end

  def add_criteria
    criteria = create_criteria(@query, request[:column_name])
    @query<<criteria

    session[:query][FILTERED_MODEL] = @query

    render :update do |page|
      page.insert_html :top, 'criteries', :partial => 'layouts/filter/criteria',
                                          :locals => { :criteria => criteria }
    end
  end

  def del_criteria
    @query.del params[:name]
    session[:query][FILTERED_MODEL] = @query

    render :update do |page|
      page.remove "criteria_#{params[:name]}"
    end
  end

  # GET /tasks/1
  # GET /tasks/1.xml

  def show
    @task = Task.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @task }
    end
  end

  # GET /tasks/new
  # GET /tasks/new.xml
  def new
    @task = Task.new
    @users = User.find :all
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @task }
    end
  end

  # GET /tasks/1/edit
  def edit
    @task = Task.find(params[:id])
  end

  # POST /tasks
  # POST /tasks.xml
  def create
    @task = Task.new(params[:task])

    respond_to do |format|
      if @task.save
        flash[:notice] = 'Task was successfully created.'
        format.html { redirect_to(@task) }
        format.xml  { render :xml => @task, :status => :created, :location => @task }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @task.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /tasks/1
  # PUT /tasks/1.xml
  def update
    @task = Task.find(params[:id])

    respond_to do |format|
      if @task.update_attributes(params[:task])
        flash[:notice] = 'Task was successfully updated.'
        format.html { redirect_to(@task) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @task.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /tasks/1
  # DELETE /tasks/1.xml
  def destroy
    @task = Task.find(params[:id])
    @task.destroy

    respond_to do |format|
      format.html { redirect_to(tasks_url) }
      format.xml  { head :ok }
    end
  end

end

